package com.ankur.calllogger.dao;

import java.util.List;

import javax.persistence.Id;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Unindexed;

@Cached
public class Account {
	@Id
	private String email;
	@Unindexed
	private String userId;
	@Unindexed
	private String name;
	
	public Account(){
		super();
	}
	
	public Account(String email, String userId, String name){
		this.email=email;
		this.userId=userId;
		this.name=name;
	}
	
	public String getEmail() {
		return email;
	}
	
	private static Objectify getService() {
		return ObjectifyService.begin();
	}
	
	public static Account findByEmail(String email) {
		Objectify service=getService();
		return service.get(Account.class,email);
	}
	
	public void addCallDetail(CallDetail callDetail) {
		callDetail.setOwner(this);
		Objectify service=getService();
		service.put(callDetail);
	}
	
	public void addCallDetails(List<CallDetail> callDetails){
		for(CallDetail callDetail : callDetails) {
			callDetail.setOwner(this);
		}
		Objectify service=getService();
		service.put(callDetails);
	}
	
	public List<CallDetail> listAllCallDetailByDate() {
		Objectify service=getService();
		return service.query(CallDetail.class).filter("owner", this).order("-date").list();
	}
	
	public void save() {
		Objectify service=getService();
		service.put(this);
	}
	
	public void deleteAll() {
		Objectify service=getService();
//		service.query(CallDetail.class).fetchKeys();
		service.delete(service.query(CallDetail.class).fetchKeys());
//		ofy.delete(allKeys);
	}
}
